﻿using HandyControl.Controls;
using HandyControl.Data;
using System.Windows;
using System.Windows.Media;
using Application = System.Windows.Application;

namespace BloodApp.Common
{
    public class InfoShowService
    {
        public static void Success(string message = "", int time = 1)
        {
            if (string.IsNullOrEmpty(message))
                message = "操作成功！";
            Application.Current.Dispatcher.Invoke(() =>
            {
                Growl.SuccessGlobal(new GrowlInfo() { Message = message, IsCustom = true, WaitTime = time });
            });
        }
        public static void Info(string message = "", int time = 1)
        {
            if (string.IsNullOrEmpty(message))
                message = "操作完成！";
            Application.Current.Dispatcher.Invoke(() =>
            {
                Growl.SuccessGlobal(new GrowlInfo() { Message = message, IsCustom = true, WaitTime = time });
            });
        }
        public static void Error(string message = "", int time = 2)
        {
            if (string.IsNullOrEmpty(message))
                message = "操作失败！";
            Application.Current.Dispatcher.Invoke(() =>
            {
                Growl.ErrorGlobal(new GrowlInfo() { Message = message, IsCustom = true, WaitTime = time });
            });
        }
        public static void Warn(string message = "", int time = 2)
        {
            if (string.IsNullOrEmpty(message))
                message = "操作故障！";
            Application.Current.Dispatcher.Invoke(() =>
            {
                Growl.WarningGlobal(new GrowlInfo() { Message = message, IsCustom = true, WaitTime = time });
            });
        }
        public static void ErrorAlarm(string message = "")
        {
            if (string.IsNullOrEmpty(message))
                message = "操作失败！";
            MediaPlayer player = new MediaPlayer();
            string path = AppDomain.CurrentDomain.BaseDirectory + @"alert1.mp3";
            player.Volume = 1;
            player.SpeedRatio = 1;
            player.Open(new Uri(path));
            player.MediaEnded += (sender, e) =>
            {
                player.Position = new TimeSpan(0);
            };
            player.Play();
            Application.Current.Dispatcher.Invoke(() =>
            {
                Growl.ErrorGlobal(new GrowlInfo()
                {
                    Message = message,
                    ActionBeforeClose = isConfirmed =>
                    {
                        player.Close();
                        return true;
                    }
                });
            });
        }
    }
}
